package com.cxhome.cxinfox.order;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Enumeration;

import com.cxhome.cxinfox.customer.CustomerService;
import com.cxhome.cxinfox.provider.ProviderService;
import com.jfinal.plugin.activerecord.Page;
import com.traffic.managent.annotation.ControllerBind;
import com.traffic.managent.base.CxBaseController;
import com.traffic.managent.jedis.Redis;
import com.traffic.managent.jedis.running.OrderEveryExecutor;
import com.traffic.managent.model.system.Department;
import com.traffic.managent.model.system.User;
import com.traffic.managent.service.order.bean.OrderBean;
import com.traffic.managent.util.StringUtils;

/**
 * 订单管理管理
 * 
 * @author Guoxing
 *
 */
@ControllerBind(controllerKey = "/system/orderinfo")
public class OrderController extends CxBaseController {

	private String path = "/pages/system/orderinfo/";

	public void index() {
		queryListForErr();
	}

	/**
	 * 流量查询 ：提供平台人员查看
	 */
	public void queryListForCus() {
		path += "orderinfoForCus_";
		Order model = getModelByAttr(Order.class);
		String customerId = getSessionUser().getStr("customerId");
		String departmentId = getSessionUser().getStr("departmentId");
		StringBuilder orderSql = new StringBuilder("");
		String start = getPara("startTime");
		String end = getPara("endTime");

		// 排序
		String orderBy = getBaseForm().getOrderBy();
		if (StringUtils.isEmpty(orderBy)) {
			orderSql.append(" order by  o.orderTime desc");
		} else {
			orderSql.append(" order by o." + orderBy.trim());
		}

		Page<Order> page = OrderService.service.queryListForCus(model, getPaginator(), orderSql, customerId,
				departmentId, start, end);

		setAttr("startTime", getPara("startTime"));
		setAttr("endTime", getPara("endTime"));
		setAttr("page", page);
		setAttr("attr", model);
		render(path + "list.html");
	}
	
	

	public void getCusView() {
		Order model = Order.dao.findById(getPara());
		setAttr("model", model);

		render(path + "orderinfoForCus_view.html");
	}

	/**
	 * 异常订单管理
	 * 
	 */
	public void queryListForErr() {
		Order model = getModelByAttr(Order.class);
		path += "orderinfoForErr_";

		// 当前登录用户所属组织机构
		String departmentId = getSessionUser().getStr("departmentId");
		String customerId = getSessionUser().getStr("customerId");
		StringBuilder orderSql = new StringBuilder("");
		String start = getPara("startTime");
		String end = getPara("endTime");

		// 排序
		String orderBy = getBaseForm().getOrderBy();
		if (StringUtils.isEmpty(orderBy)) {
			orderSql.append(" order by  o.orderTime desc");
		} else {
			orderSql.append(" order by " + orderBy.trim());
		}

		Page<Order> page = OrderService.service.queryListForErr(model, getPaginator(), orderSql, customerId,
				departmentId, start, end);

		setAttr("selectCustomer", CustomerService.service.selectCustomer(model.getStr("customerId")));
		setAttr("startTime", getPara("startTime"));
		setAttr("endTime", getPara("endTime"));
		setAttr("page", page);
		setAttr("attr", model);
		render(path + "list.html");

	}
	
	public void getErrView() {
		Order model = Order.dao.findById(getPara());
		setAttr("model", model);
		render(path + "orderinfoForCus_view.html");
//		render(path + "orderinfoForErr_view.html");
	}

	/**
	 * 订单查询
	 */
	public void queryListForVas() {

		Order model = getModelByAttr(Order.class);
		path += "orderinfoForVas_";
		// 数据权限
		String departmentId = getSessionUser().getStr("departmentId");
		StringBuilder orderSql = new StringBuilder("");
		String start = getPara("startTime");
		String end = getPara("endTime");

		// 排序
		String orderBy = getBaseForm().getOrderBy();
		if (StringUtils.isEmpty(orderBy)) {
			orderSql.append(" order by  o.orderTime desc");
		} else {
			orderSql.append(" order by " + orderBy.trim());
		}
		Page<Order> page = OrderService.service.queryListForVas(model, getPaginator(), orderSql, departmentId, start,
				end);

		setAttr("selectCustomer", CustomerService.service.selectCustomer(model.getStr("customerId")));
		setAttr("selectProvider", ProviderService.service.selectProvider(model.getStr("providerId")));
		setAttr("startTime", getPara("startTime"));
		setAttr("endTime", getPara("endTime"));
		setAttr("page", page);
		setAttr("attr", model);
		render(path + "list.html");

	}

	public void getVasView() {
		Order model = Order.dao.findById(getPara());
		setAttr("model", model);
		render(path + "orderinfoForCus_view.html");
//		render(path + "orderinfoForVas_view.html");
	}

	/**
	 * 向该上游做一次查询，如有订单，则根据该订单改变我们状态， 没有订单 则再次订购
	 */
	public void doQuery() {
		Order order = Order.dao.findById(getPara());

		OrderBean orderBean = new OrderBean(order);
		// 订单放入延时队列
		log.info("该订单执行请求上游的订单号：" + orderBean.getIds());
		// 延迟队列缓存
		String orderKey = "delay_" + orderBean.getIds();
		Redis.use("redisOrderPlugin").set(orderKey, orderBean);
		// 放入延迟缓存队列中执行
		OrderEveryExecutor.service.setOrder(orderBean);
		queryListForErr();
	}

	/**
	 * 订购：再次向上游定购
	 */
	public void doOrder() {
		Order order = Order.dao.findById(getPara());
		OrderBean orderBean = new OrderBean(order);
		orderBean.setMessage("无订单");
		// 将订单重新放入队列中去开通
		OrderEveryExecutor.service.setOrder(orderBean);
		queryListForErr();
	}

	/*
	 * 页面手动失败：订单状态设置为8
	 */

	public void setFail() {
		Order order = Order.dao.findById(getPara());
		OrderBean orderBean = new OrderBean(order);
		orderBean.setStatus("8");
		OrderEveryExecutor.service.setOrder(orderBean);
		queryListForErr();
	}

	/*
	 * 页面手动成功：订单状态设置为7
	 */

	public void setSucc() {
		// String ids = getPara();
	}
}
